package com.lin.activity;

import java.util.ArrayList;

import com.lin.utils.Session;
import com.lin.utils.Session.ResultReceiver;
import com.lin.views.ListViewForSearchFriend;
import com.ly.quickdev.library.view.MySideBar.OnTouchingLetterChangedListener;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.ShareActivity;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.bean.UserBean;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class AddFriendActivity extends Activity implements OnClickListener, OnTouchListener, TextWatcher, OnItemClickListener{
	LinearLayout mLinearLayoutScan;
	private LinearLayout mLinearLayoutNearBy;
	private EditText mEditTextSearch;
	private FrameLayout mFrameLayoutMore;
	private FrameLayout mFrameLayoutBack;
	private LinearLayout mLinearLayout;
	private ListViewForSearchFriend listview;
	Session mSession;
	UserBean mUserBean;
	SuperFriendsBean mCurData;
	private LinearLayout mLinearLayoutEntreprise;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_friends);
		mUserBean = BaseActivity.getUser(getApplicationContext());
		
		mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout);
		listview = (ListViewForSearchFriend) findViewById(R.id.listview);
		mLinearLayoutScan = (LinearLayout) findViewById(R.id.linearlayout_scan);
		mLinearLayoutNearBy = (LinearLayout) findViewById(R.id.linearlayout_nearby);
		mLinearLayoutEntreprise = (LinearLayout) findViewById(R.id.linearlayout_entreprise_nearby);
		
		mEditTextSearch = (EditText) findViewById(R.id.edittext);
		mFrameLayoutMore = (FrameLayout) findViewById(R.id.framelayout_more);
		mFrameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);
		
		mLinearLayoutEntreprise.setOnClickListener(this);
		mFrameLayoutBack.setOnClickListener(this);
		mFrameLayoutMore.setOnClickListener(this);
		mLinearLayoutScan.setOnClickListener(this);
		mLinearLayoutNearBy.setOnClickListener(this);
		mEditTextSearch.addTextChangedListener(this);
		listview.setOnItemClickListener(this);
		mSession = Session.getSession();
		mSession.init(getApplicationContext(), true);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == mLinearLayoutNearBy) {
			Intent intent = new Intent(this, SuperFriendsNearByActivity.class);
			startActivity(intent);
		} else if(v == mLinearLayoutScan) {
			Intent intent = new Intent(this, DecoderActivity.class);
			startActivity(intent);
		} else if(mFrameLayoutBack == v) {
			finish();
		} else if(mFrameLayoutMore == v) {
			Intent intent = new Intent(this, ShareActivity.class);
			startActivity(intent);
		} else if(mLinearLayoutEntreprise == v) {
			startActivity(new Intent(this, EntrepriseNearByActivity.class));
		}
		
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		String result = arg0.toString();
		int resUserId;
		if(result.length() == 0) {
			mLinearLayout.setVisibility(View.VISIBLE);
			listview.setVisibility(View.INVISIBLE);
		} else {
			mLinearLayout.setVisibility(View.INVISIBLE);
			listview.setVisibility(View.VISIBLE);
		}
		
		boolean isNum = result.matches("[0-9]+");
		if(isNum == true && result.length() < 9) {
			resUserId = Integer.parseInt(result);
			
		} else {
			
			resUserId = -1;
		}
		mSession.searchUser(getApplicationContext(), result, mUserBean.getType(), resUserId, mUserBean.getLat(), mUserBean.getLng(), new ResultReceiver<SuperFriendsBean>() {

			@Override
			public void onResult(boolean last, SuperFriendsBean data) {
				// TODO Auto-generated method stub
				if( data != null ) {
				ArrayList<SuperFriendsBean> mList = new ArrayList<SuperFriendsBean>();
				mList.add(data);
				listview.setDataList(mList);
				mCurData = data;
				}
			}
			
		});
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
	
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		if(mCurData != null) {
			Intent intent = new Intent(this, PersonalDetailCardActivity.class);
			intent.putExtra("data", mCurData);
			startActivity(intent);
		}
	}
	
}